//
//  JSMLeavingHeaderCollectionDetailsViewController.m
//  TakeYouTourTogether
//
//  Created by dllo on 15/12/2.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMLeavingHeaderCollectionDetailsViewController.h"
#import "JSMLeavingHeaderShowCityDetailsModel.h"
#import "JSMLeavingHeaderShowCityDetailsToolsModel.h"
#import "JSMLeavingHeaderShowCityDetailsHottestModel.h"
#import "JSMLeavingHeaderCollectionDetailsTableViewCell.h"
@interface JSMLeavingHeaderCollectionDetailsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong) BaseTableView *headerCollectionDetailsTableView;
@property(nonatomic, strong) JSMLeavingHeaderShowCityDetailsModel *detailsModel;
@property(nonatomic, strong) NSMutableArray *cityToolsMarr;
@property(nonatomic, strong) NSMutableArray *cityHottestMarr;
@property(nonatomic, strong) MBProgressHUD *hud;
@end

@implementation JSMLeavingHeaderCollectionDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.showCityModel.name;
    [self createHeaderCollectionDetailsTableView];
    [self createRightButton];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.hud.labelText = @"Loading...";
    [self createData];
}

- (void)createRightButton {
    BaseButton *rightButton = [BaseButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 40, 40);
    if ([SaveTool isHaveModelInPlist:self.showCityModel type:SaveLeavingModel]) {
        [rightButton setImage:[UIImage imageNamed:SAVE_NOTHAVE_IMAGE] forState:UIControlStateNormal];
    } else {
        [rightButton setImage:[UIImage imageNamed:SAVE_ISHAVE_IMAGE] forState:UIControlStateNormal];
    }
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    [rightButton addTarget:self action:@selector(saveAction:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)saveAction:(BaseButton *)button {
    NSString *message;
    if ([button.imageView.image isEqual:[UIImage imageNamed:SAVE_ISHAVE_IMAGE]]) {
        [SaveTool cancelSaveModelInPlist:self.showCityModel type:SaveLeavingModel];

//        [button setImage:[UIImage imageNamed:SAVE_NOTHAVE_IMAGE]];
        [button setImage:[UIImage imageNamed:SAVE_NOTHAVE_IMAGE] forState:UIControlStateNormal];
         message = @"取消收藏";
    } else {
        [SaveTool saveModelToPlist:self.showCityModel type:SaveLeavingModel];
//        [button setImage:[UIImage imageNamed:SAVE_ISHAVE_IMAGE]];
        [button setImage:[UIImage imageNamed:SAVE_ISHAVE_IMAGE] forState:UIControlStateNormal];
        message = @"已收藏";
    }
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alert animated:YES completion:^{
        
    }];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)createData {
    NSString *showDetailsUrl = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/3/%@/", self.showCityModel.cityId];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"text/json", @"text/javascript", @"text/html", @"application/json", nil];
    [manager.requestSerializer setValue:@"BreadTrip/android/6.1.1/zh (android OS4.3) armani Map/GoogleMap/v1 (HM 1SC,Xiaomi) Paros/3.2.13" forHTTPHeaderField:@"User-Agent"];
    [manager GET:showDetailsUrl parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (responseObject != nil) {
            self.detailsModel = [[JSMLeavingHeaderShowCityDetailsModel alloc] init];
            [self.detailsModel setValuesForKeysWithDictionary:responseObject];
            self.cityToolsMarr = [NSMutableArray array];
            self.cityToolsMarr = self.detailsModel.toolsMarr;
            self.cityHottestMarr = [NSMutableArray array];
            self.cityHottestMarr = self.detailsModel.hottest_sitesMarr;
            
            // 缓存
        if (self.cityHottestMarr.count > 0 && self.cityToolsMarr.count > 0) {
                    [self.headerCollectionDetailsTableView reloadData];
                }
            [self.hud removeFromSuperview];

        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

    
}

- (void)createHeaderCollectionDetailsTableView {
    self.headerCollectionDetailsTableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.headerCollectionDetailsTableView];
    self.headerCollectionDetailsTableView.dataSource = self;
    self.headerCollectionDetailsTableView.delegate = self;
    self.headerCollectionDetailsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.cityHottestMarr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [self.cityHottestMarr[section] name];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
    CGRect wordRect = [[self.cityHottestMarr[indexPath.section] descriptionStr]boundingRectWithSize:CGSizeMake(SCREENWIDTH - 40, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return (SCREENWIDTH - 30) * 2 / 3 + wordRect.size.height + 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    JSMLeavingHeaderCollectionDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[JSMLeavingHeaderCollectionDetailsTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:[self.cityHottestMarr[indexPath.section] cover_route_map_cover]] placeholderImage:PlaceHolderImage];
    cell.descriptionLabel.text = [NSString stringWithFormat:@" %@ ", [self.cityHottestMarr[indexPath.section] descriptionStr]];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    return cell;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
